package com.ssh.cp.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.ssh.cp.entity.User;
import com.ssh.cp.iservice.IUserService;

import net.sf.json.JSONObject;


//注解方式配置Action
//@Results( { @Result(name="success",location="/success.jsp"),  
//    @Result(name="failure",location="/failure.jsp") })   


@Controller
public class UserAction{  

	
@Autowired  
private IUserService userManager;  
private Map<String,Object> request,session;
private ActionContext ctx;
private User user; 
private String birthday,nickname;

private String oldPassword,newPassword1,newPassword2;
private InputStream resultInputStream;


JSONObject libJSON;

public JSONObject getLibJSON() {
	return libJSON;
}


public void setLibJSON(JSONObject libJSON) {
	this.libJSON = libJSON;
}


//@Action(value="addUser")  
@SuppressWarnings("unchecked")
public String addUser() {
	ctx=ActionContext.getContext();
	request=(Map<String, Object>)ctx.get("request");
	String r="";
	if(user.getUsername()==""||user.getPassword()=="") {
		request.put("tip", "用户名或密码不能为空！");
		return "failure";  
	}
	if(user.getUsername().matches("[0-9]+")) {
		request.put("tip", "用户名不能为纯数字！");
		return "failure"; 
	}
	

	
    try {
    	//User u=new User(user.getUsername(),user.getPassword());
    	
    	System.out.println(user.getUsername()+"  "+user.getPassword());
    	user.setRoleid(2);
       r= userManager.addUser(user);          
    } catch (Exception e) {  
        e.printStackTrace(); 
        request.put("tip", r);
        return "failure";  
    }  
    request.put("tip", r);
    return "success";  
      
}      


//@Action(value="login")
@SuppressWarnings("unchecked")
public String login() {
	ctx=ActionContext.getContext();
	session=(Map<String, Object>)ctx.getSession();
	request=(Map<String, Object>)ctx.get("request");
	
	User usr=null;
	List<?> list=userManager.login(user);
	if(!list.isEmpty())
		usr=(User)list.get(0);
	
	if(usr==null) {
		request.put("tip", "用户不存在或用户名密码错误！");
		return "failure";
	}else {
		if(usr.getForbid()==1) {
			request.put("tip", "您已被管理员禁止登陆！");
			return "failure";
		}else {
		
		//ServletActionContext.getContext().getSession().put("username", usr.getUsername());

		request.put("tip", "login success!");
		session.put("username", usr.getUsername());
		session.put("nickname", usr.getNickname());
		session.put("userid", usr.getUserid());
		session.put("role", usr.getRoleid());
		session.put("birthday", usr.getBirthday());
		session.put("createTime", usr.getCreateTime());
		//int userid=Integer.valueOf(usr.getUserid());
		//System.out.println("==========userid: "+userid);
		//JSONArray libArray=JSONArray.fromObject();
		if(usr.getRoleid()==1)
			return "admin";
		else
			return "success";
	}
	}
	
}



public String updateUser() {
	ctx=ActionContext.getContext();
	int userid=Integer.valueOf(ctx.getSession().get("userid").toString());
	
	
	//System.out.println(nickname);
	userManager.updateUser(userid,birthday,nickname);
	return "success";
}


public String changePassword() throws UnsupportedEncodingException {
	ctx=ActionContext.getContext();
	int userid=Integer.valueOf(ctx.getSession().get("userid").toString());
	if(oldPassword==null||"".equals(oldPassword)) {
		setResultInputStream(new ByteArrayInputStream("原密码不能为空！".getBytes("UTF-8")));
		return "fail";
	}else {
		if(!newPassword1.equals(newPassword2)) {
			setResultInputStream(new ByteArrayInputStream("两次输入密码不匹配！".getBytes("UTF-8")));
			return "fail";
		}else {
			String re=userManager.changePassword(userid, oldPassword, newPassword2);
			System.out.println(re);
			setResultInputStream(new ByteArrayInputStream(re.getBytes("UTF-8")));
			return "success";
		}
	}
	
	//System.out.println(nickname);
	//userManager.updateUser(userid,birthday,nickname);
	
}




public User getUser() {  
    return user;  
}  
public void setUser(User user) {  
    this.user = user;  
}





public String getBirthday() {
	return birthday;
}


public void setBirthday(String birthday) {
	this.birthday = birthday;
}


public String getNickname() {
	return nickname;
}


public void setNickname(String nickname) {
	this.nickname = nickname;
}


public InputStream getResultInputStream() {
	return resultInputStream;
}


public void setResultInputStream(InputStream resultInputStream) {
	this.resultInputStream = resultInputStream;
}

public String getOldPassword() {
	return oldPassword;
}


public void setOldPassword(String oldPassword) {
	this.oldPassword = oldPassword;
}


public String getNewPassword1() {
	return newPassword1;
}


public void setNewPassword1(String newPassword1) {
	this.newPassword1 = newPassword1;
}


public String getNewPassword2() {
	return newPassword2;
}


public void setNewPassword2(String newPassword2) {
	this.newPassword2 = newPassword2;
}


  
}  